<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { orderList, orderUpdate } from '../../../api/index'
import { InfoFilled,Delete } from '@element-plus/icons-vue'

// 路由数据
const route = useRoute()

// 分页数据
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
  out_trade_no: ''
})
// 搜索表单数据
const searchForm = reactive({
  out_trade_no: ''
})

// 菜单列表数据
let tableData = reactive({
  list: [],
  total: 0,
})

// 请求列表数据
async function getListData(params ={}) {
  const {
    data: { data },
  } = await orderList({...paginationData, ...params})
  tableData.list = data.list
  tableData.total = data.total
  
}

onMounted(() => {
  getListData()
})

// 映射
const statusMap = (key) => {
  const obj = {
    '已取消': 'info',
    '待支付': 'warning',
    '已完成': 'success'
  }

  return obj[key]
}

// 确认服务完成
async function confirmEvent(orderId) {
  const {data} = await orderUpdate({id: orderId})
  console.log(data)
  if(data.code === 10000) {
    // 刷新列表
    getListData()
  }
}

// 搜索列表
function onSubmit() {
  getListData(searchForm)
}

// 切换页数
function handleSizeChange(val) {
  paginationData.pageSize = val
  getListData()
}
// 翻页
function handleCurrentChange(val) {
  paginationData.pageNum = val
  getListData()
}
</script>

<template>
  <!-- 面包屑 -->
  <PanelHead :routeData="route"></PanelHead>
  <!-- 搜索 -->
  <div class="form">
    <el-form :inline="true" :model="searchForm">
      <el-form-item prop="out_trade_no">
        <el-input v-model="searchForm.out_trade_no" placeholder="请输入订单号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form> 
  </div>
  <!-- 表格 -->
   <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="out_trade_no" fixed="left" width="150" label="订单号"></el-table-column>
    <el-table-column prop="hospital_name" label="就诊医院"></el-table-column>
    <el-table-column prop="service_name" label="陪诊服务"></el-table-column>
    <el-table-column prop="name" label="陪诊师">
      <template #default="scope">
        <el-avatar 
          :src="scope.row.companion.avatar"
          ></el-avatar>
      </template>
    </el-table-column>
    <el-table-column width="120" prop="companion.mobile" label="陪诊师手机号">
    </el-table-column>
    <el-table-column prop="price" label="总价"></el-table-column>
    <el-table-column prop="paidPrice" label="已支付"></el-table-column>
    <el-table-column prop="starttime" label="下单时间"></el-table-column>
    <el-table-column label="订单状态">
      <template #default="scope">
        <el-tag :type="statusMap(scope.row.trade_state)">
          {{scope.row.trade_state}}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="service_state" label="接单状态"></el-table-column>
    <el-table-column width="120" prop="tel" label="联系人手机号"></el-table-column>
    <el-table-column prop="name" label="操作" fixed="right">
      <template #default="scope">
       <el-popconfirm
        v-if="scope.row.trade_state === '待服务'"
        confirm-button-text="是"
        cancel-button-text="否"
        :icon="InfoFilled"
        icon-color="#626aef"
        title="是否确认完成?"
        @confirm="confirmEvent(scope.row.out_trade_no)"
        > 
          <template #reference>
            <el-button :icon="Delete" type="primary" size="small" link>服务完成</el-button>
          </template>
       </el-popconfirm>
       <el-button v-else type="primary" link disabled>暂无服务</el-button>
      </template>
    </el-table-column>
  </el-table>

  <div class="paginatin-info">
    <!-- 分页器 -->
    <el-pagination
      :page-size="paginationData.pageSize"
      :background="false"
      size="small"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<style scoped lang="less">
.paginatin-info {
  background-color: #fff;
  height: 40px;
  .el-pagination {
    height: 100%;
    box-sizing: border-box;
    padding: 0 10px;
  }
}

.form {
  display: flex;
  justify-content: flex-end;
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
</style>
